<?php


namespace custom;


class GetVid {

    //安居客地址匹配小区
    const API_URL_HOST = [
        '成都市' => ['url' => "https://chengdu.anjuke.com/", 'city_id' => 15],
        '重庆市' => ['url' => "https://chongqing.anjuke.com/", 'city_id' => 20],
        '青岛市' => ['url' => "https://qd.anjuke.com/", 'city_id' => 30],
        '武汉市' => ['url' => "https://wuhan.anjuke.com/", 'city_id' => 22],
        '长沙市' => ['url' => "https://cs.anjuke.com/", 'city_id' => 27],
        '广州市' => ['url' => "https://guangzhou.anjuke.com/", 'city_id' => 12],
        '德阳市' => ['url' => "https://deyang.anjuke.com/", 'city_id' => 97],
        '绵阳市' => ['url' => "https://mianyang.anjuke.com/", 'city_id' => 61],
        '西安市' => ['url' => "https://xa.anjuke.com/", 'city_id' => 31],
        '兰州市' => ['url' => "https://lanzhou.anjuke.com/", 'city_id' => 58]
    ];

    /**
     * @param int $mon_house_id mongodb表对应id
     * @param string $city_name 城市名称（市）
     * @param string $address 地址
     * @return mixed
     */
    public static function getVillageInfo($mon_house_id = 0, $city_name = '', $address = '') {
        $params['kw'] = self::dealAddress($address);
        $kw = urlencode($params['kw']);
        $api_host = self::API_URL_HOST[$city_name];
        $url = $api_host['url'];
        $city_id = urlencode($api_host['city_id']);
        $api_url = "{$url}esf-ajax/community/pc/autocomplete?city_id={$city_id}&kw={$kw}";
        $ajk_village = xhr_proxy($api_url, 'GET');
        $result = json_decode($ajk_village, true);
        Log::write("_id->{$mon_house_id} | {$city_name} | address1->{$address} | address2->{$params['kw']} | " . json_encode($result, JSON_UNESCAPED_UNICODE), "address_match_village.log");
        $data['price'] = 0;//小区均价
        $data['village_id'] = 0;//小区id
        //解析成功
        if ($result['status'] == "ok" && !empty($result['data'])) {
            $vill_name = replaceSpecialChar($result['data'][0]['name']);
            $price = $result['data'][0]['price'] ?? 0;
            //通过城市名和小区名匹配小区
            $village_info = self::getVillInfo($vill_name, $city_name);
            if ($village_info) {
                //更新小区均价
                if (!empty($price) && $village_info['price_unit_avg'] != $price) {
                    db("house_village")->where('id', $village_info['id'])->update(['price_unit_avg' => $price]);
                }
                $data['price'] = $price;//小区均价
                $data['village_id'] = $village_info['id'];//小区id
            } else {
                Log::write("小区名->{$vill_name} | {$city_name}", "notExistsVillage.log");
            }
        }
        return $data;
    }

    /**
     * 地址处理
     * @param $str
     * @return string
     */
    public static function dealAddress($str) {
        if (strstr($str, "区") !== false) {
            $t1 = mb_strpos($str, '区') + 1;
        } else {
            $t1 = 0;
        }
        if (strstr($str, "号") !== false) {
            $t2 = mb_strpos($str, '号') + 1;
        } else {
            $t2 = 0;
        }
        if ($t1 && !$t2) {
            $address = mb_substr($str, $t1);
            return $address;
        } elseif (!$t1 && $t2) {
            $address = mb_substr($str, 0, $t2);
            return $address;
        } elseif ($t1 && $t2) {
            $address = mb_substr($str, $t1, $t2 - $t1);
            return $address;
        } else {
            return $str;
        }
    }

    /**
     * 获取小区id
     * @param $name
     * @param $city_codes
     * @param $price
     * @return mixed
     */
    public static function getVillInfo($name, $city_name) {
        $where[] = ['name', '=', trim($name)];
        $where[] = ['city', '=', trim($city_name)];
        return db("house_village")->where($where)->field('id,price_unit_avg')->find();
    }

}